<?php
class Admin_SystemController extends Zend_Controller_Action
{       
    public function indexAction()
    {
        $this->view->title = 'Welcome to Wmp titre!';
        $this->view->headTitle('Welcome');
	
        $this->view->content="hello";
    }
    
    public function updateModulesActionsAction()
    {
    	$systemModules = Wmp_File::getDirectories(zend_registry::get('config')->path->modules->dir);
    	$regestredModules = new Wmp_Model_ModulesTable();
    	$allActions = new Wmp_Model_ActionparamsTable();
    	
    	foreach($systemModules as $module) {
    		if($regestredModules->find($module)->count()==0)
    			$regestredModules->insert(array('name'  => $module));
    		
    		$controllersDir = zend_registry::get('config')->path->modules->dir.	'/'.$module.'/controllers';
    		
    		if(is_dir($controllersDir)) {
    			$systemControllers = Wmp_File::getFiles($controllersDir);
    			$moduleControllersActons = new ActionsDetector($controllersDir,$systemControllers);    			
    			$moduleControllersActons = $moduleControllersActons->getActions();
    			
	    		foreach($moduleControllersActons as $controller => $actions) {
	    			foreach($actions as $key => $action)
	    			if(!$allActions->getRow($module,$controller,$action)) {
	    				$allActions->insert(array('module'  => $module,
	    										  'controller' => $controller,
	    				                           'action' => $action));
	    			}
	    		}
    		}
    	}		
       	$this->_forward('modules');
    }

	public function templatesAction()
    {
    	
    }
    
	public function templateUpdateAction()
    {
    	
    }
    
    public function modulesAction()
    {
    	$regestredModules = new Wmp_Model_ModulesTable();    			
    	$this->view->modules = $regestredModules->fetchAll();
    }

	public function moduleUpdateAction()
    {
    	if($this->_hasParam('name')) { 
    		$this->language =  zend_registry::get('Zend_Translate')->getLocale();
    		$modulename =  $this->_getParam('name');
    		$formAction = $this->view->url( array( "language" =>$this->language, 
								"module"=>"admin",
								"controller"=>"system",
								"action"=>"module-update",
								"name"=>$modulename) );
    		$form = new ModuleUpdateForm();
    		$form->setAction($formAction);
    	}
    	else return;
    	
    	$regestredModules = new Wmp_Model_ModulesTable();
    	
    	$moduleData = $regestredModules->fetchRow($regestredModules->select()->where('name = ?', $modulename));
    	
    	if($this->_request->isPost() && $form->isValid($_POST)){
    		
    		$moduleData->title=$this->_getParam('title');
    		$moduleData->description=$this->_getParam('description');
    		$moduleData->metakeywords=$this->_getParam('metakeywords');
    		$moduleData->metadescription=$this->_getParam('metadescription');
    		$moduleData->save(); 
   
			$this->_forward('modules');       
    	}
    		    	
		$form->populate($moduleData->toArray());
    	$this->view->form = $form;
    	$this->view->modulename=$modulename;	
    }
    
	public function actionsAction()
    {
    	if(!$this->_hasParam('name')) return;
    	
    	$moduleName = $this->_getParam('name');
    	$moduleActions = new Wmp_Model_ActionparamsTable();
    	
    	$this->view->actions = $moduleActions->getActions($moduleName);
    }
        
    public function actionUpdateAction()
    {
		if($this->_hasParam('modname') && $this->_hasParam('actname') && $this->_hasParam('contname')) { 
    		$this->language =  zend_registry::get('Zend_Translate')->getLocale();
    		$modulename =  $this->_getParam('modname');
    		$controllername =  $this->_getParam('contname');
    		$actionname =  $this->_getParam('actname');
    		
    		$formAction = $this->view->url( array( "language" =>$this->language, 
								"module"=>"admin",
								"controller"=>"system",
								"action"=>"action-update" ) );
    		$form = new ActionUpdateForm();
    		$form->setAction($formAction);
    	}
    	else return;
    	
    	$actions = new Wmp_Model_ActionparamsTable();    	
    	$actionData = $actions->getRow($modulename,$controllername,$actionname);
    	
    	if($this->_request->isPost() && $form->isValid($_POST)){
    		
    		$actionData->title=$this->_getParam('title');
    		$actionData->description=$this->_getParam('description');
    		$actionData->metakeywords=$this->_getParam('metakeywords');
    		$actionData->metadescription=$this->_getParam('metadescription');
    		$actionData->menuid=$this->_getParam('menuid');
    		$actionData->menuposition=$this->_getParam('menuposition');
    		$actionData->layout=$this->_getParam('layout');
    		$actionData->save(); 
   
			$redirectUrl = $this->view->url( array( "language" =>$this->language, 
													"module"=>"admin",
													"controller"=>"system",
													"action"=>"actions",
													"name"=>$modulename ) );

			header('Location: '.$redirectUrl);
			exit;
    	}
    		    	
		$form->populate($actionData->toArray());
    	$this->view->form = $form;
    	$this->view->actionname =  $this->view->translate('Module : ').$modulename.', '.$controllername.':'.$actionname;   	
    }
}